package com.iraeta.service.sys.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iraeta.Config;
import com.iraeta.common.Constants;
import com.iraeta.entity.SysFiles;
import com.iraeta.mapper.sys.SysFilesMapper;
import com.iraeta.service.sys.ISysFilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * 文件上传Service业务层处理
 * 
 * @author iraeta
 * @date 2023-11-04
 */
@Service
public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFiles> implements ISysFilesService
{
    @Autowired
    Config config;

    public List<SysFiles> selectSysFilesList(Integer entityTypeId, Long entityId)
    {
        SysFiles sysFiles = new SysFiles(entityTypeId, entityId);
        return baseMapper.selectSysFilesList(sysFiles);
    }
    @Override
    public void deleteSysFilesByFId(Long fId)
    {
        SysFiles sysFiles = baseMapper.selectById(fId);
        String realFilePath = config.profile + sysFiles.getFilePath().substring( Constants.RESOURCE_PREFIX.length());
        File deleteFile = new File(realFilePath);
        if(deleteFile.exists()){
            deleteFile.delete();
        }
        baseMapper.deleteById(fId);
    }
    @Override
    public void updateSysFilesEntityId(Long oldEntityId, Long newEntityId) {
        baseMapper.updateSysFilesEntityId(oldEntityId, newEntityId);
    }
}
